package osc.ashye.sharelist;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

public class SendEmailActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_email);

        findViewById(R.id.btn_share).setOnClickListener(this);
        findViewById(R.id.btn_email).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_share:
//                shareText();
                shareImage();
//
                break;

            case R.id.btn_email:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        email("zhangwei@zxtd.net", "android send email", "this is a test email from android app testing sending email.");
                    }
                }).start();
                break;
        }
    }

    /**
     * send a share action and pass shared data
     */
    private void shareText() {
        Intent link = new Intent(Intent.ACTION_SEND);
        link.setType("text/plain");
        link.putExtra(Intent.EXTRA_TEXT, "baidu");
        link.putExtra(Intent.EXTRA_TITLE, "baidu");
        startActivity(Intent.createChooser(link, "分享"));
    }

    private void shareImage() {
        Intent image = new Intent(Intent.ACTION_SEND);
        image.setType("image/*");
        image.putExtra(Intent.EXTRA_STREAM, Uri.parse("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png"));
        image.putExtra(Intent.EXTRA_TITLE, "baidu icon");
        startActivity(Intent.createChooser(image, getResources().getText(R.string.app_name)));
    }

    /**
     * send email
     *
     */
    private void email(final String to, final String title, final String content) {
        String host = "smtp.qq.com";
        String port = "25";
        String sender = "ashye@vip.qq.com";
        String from = sender;
        String pass = "47260520mn";

        try {
            sendEmail(host, port, sender, pass, from, to, title, content);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


    /**
     * 邮件发送程序
     *
     * @param host
     *            邮件服务器 如：smtp.qq.com
     * @param sender
     *            发送邮件的地址 如：545099227@qq.com
     * @param from
     *            来自： wsx2miao@qq.com
     * @param password
     *            您的邮箱密码
     * @param to
     *            接收人
     * @param port
     *            端口（QQ:25）
     * @param subject
     *            邮件主题
     * @param content
     *            邮件内容
     * @throws Exception
     */
    private void sendEmail(final String host, final String port,
                           final String sender, final String password,
                           final String from, final String to,
                           final String subject, final String content) throws MessagingException{

        Multipart multipart;
        String finalString = "";



        Properties properties = System.getProperties();
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.user", sender);
        properties.put("mail.smtp.password", password);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(properties, null);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(finalString.getBytes(), "text/plain"));
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setDataHandler(handler);
        Log.i("Check", "done sessions");


        multipart = new MimeMultipart();
        InternetAddress toAddress;
        toAddress = new InternetAddress(to);
        message.addRecipient(Message.RecipientType.TO, toAddress);
        Log.i("Check", "added recipient");
        message.setSubject(subject);
        message.setContent(multipart);
        message.setText(content);

        Log.i("check", "transport");
        Transport transport = session.getTransport("smtp");
        Log.i("check", "connecting");
        transport.connect(host, sender, password);
        Log.i("check", "wana send");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        Log.i("check", "sent");
    }
}
